package com.app.service.impl;

import com.app.common.CalculateDistance;
import com.app.common.ConstantTool;
import com.app.mapper.jiuquan.SpecialityManageMapper;
import com.app.mapper.jiuquan.TrainMapper;
import com.app.mapper.jiuquan.TrainSpecialityMapper;
import com.app.model.SpecialityManageModel;
import com.app.model.TrainModel;
import com.app.service.TrainService;
import com.app.service.TrainSpecialityService;
import com.app.util.StringTool;
import com.app.vo.EducationVo;
import com.app.vo.TrainVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//培训机构
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, TrainModel> implements TrainService {
    @Autowired
    private TrainMapper trainMapper;

    @Autowired
    private TrainSpecialityMapper trainSpecialityMapper;

    @Autowired
    private SpecialityManageMapper specialityManageMapper;
    @Autowired
    TrainSpecialityService trainSpecialityService;

    //培训机构列表
    @Override
    public TrainVo getTrainList(Integer page, Integer limit, String trainInstitution, Integer cityId) {
        Page<TrainModel> page1 = new Page<>(page, limit);

        QueryWrapper<TrainModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(trainInstitution)) {
            qw.like("train_institution", trainInstitution);
            qw.or().like("basic_Introduction",trainInstitution);
        }
        if (cityId != null) {
            qw.eq("city", cityId);
        }
        Page<TrainModel> trainPage = trainMapper.selectPage(page1, qw);

        List<TrainModel> trainList = trainPage.getRecords();
        for (TrainModel train : trainList) {
            Integer city = train.getCity();
            List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
            for (EducationVo educationVo : areaVoList) {
                if (city == educationVo.getId()) {
                    train.setCityText(educationVo.getData());
                }
            }
        }

        long count = trainPage.getTotal();

        TrainVo trainVo = new TrainVo();
        trainVo.setList(trainList);
        trainVo.setCount((int) count);
        return trainVo;
    }

    //用户培训机构列表
    @Override
    public TrainVo getUserTrainList(Integer page, Integer limit, String trainInstitution, Double longitude, Double latitude, Integer city,Integer type) {
        List<TrainModel> trainList = trainMapper.getListByType((page - 1) * limit,limit,trainInstitution,city,type);
        long count = trainMapper.getCountByType(trainInstitution,city,type);

        //计算距离
        if (longitude != null || latitude != null) {
            for (TrainModel train : trainList) {
                Double longitudeOld = train.getLongitude();
                Double latitudeOld = train.getLatitude();
                Double number = CalculateDistance.haversine(latitudeOld, longitudeOld, latitude, longitude);
                Double distance = Double.valueOf(String.format("%.1f", number));
                train.setDistance(distance);
            }
        }

        TrainVo trainVo = new TrainVo();
        trainVo.setList(trainList);
        trainVo.setCount((int) count);

        return trainVo;
    }

    //用户培训机构详情
    @Override
    public TrainModel getUserTrainById(Integer id, Double longitude, Double latitude) {
        TrainModel train = trainMapper.selectById(id);
        //计算距离
        if (longitude != null || latitude != null) {
            Double longitudeOld = train.getLongitude();
            Double latitudeOld = train.getLatitude();
            Double number = CalculateDistance.haversine(latitudeOld, longitudeOld, latitude, longitude);
            Double distance = Double.valueOf(String.format("%.1f", number));
            train.setDistance(distance);
        }
        //创业培训
        train.setEntrepreneurshipTrain(trainSpecialityService.getTrainSpecialityList(1,100,id,1));
        //技能培训
        train.setSkillTrain(trainSpecialityService.getTrainSpecialityList(1,100,id,2));
        return train;
    }
}
